<?php
/**
 * Created by PhpStorm.
 * User: mojie126
 * Date: 2017/4/27
 * Time: 下午11:31
 */

namespace app\index\controller;

use app\common\mailqueue;
use app\common\verify;
use think\Controller;
use think\Session;
use think\Validate;

class SignupController extends Controller
{
	public function index($verify = "")
	{
		$this->assign("title", "注册");
		if (!empty(Session::get("uid"))) {
			$this->error("已经登录", "/");
		}
		if ($verify) {
			$res = verify::decode($verify);
			if (!$res) {
				$this->error("链接已失效", "/index/signup");
			} else {
				$this->assign("invitecode", $res->sub);
			}
		} else {
			$this->assign("invitecode", "");
		}

		return $this->fetch();
	}

	public function doSignup()
	{
		$info = input("param.");

		//输入验证
		$rule = [
			"username|帐号" => "require|length:2,18",
			"email|邮箱" => "require|email",
			"password1|密码" => "require|length:6,32",
			"password2" => "require|confirm:password1",
			"invitecode|邀请码" => "length:21",
			"__token__" => "require|token"
		];
		$msg = [
			"username" => "帐号长度必须在 2~18 个字符之间",
			"email" => "邮箱格式错误",
			"password1" => "密码长度必须在 6~32 个字符之间",
			"password2" => "两次密码不一致",
			"invitecode" => "邀请码不正确，且必须是21个字符",
			"__token__" => "请勿重复提交"
		];
		$data1 = [
			"username" => $info['username'],
			"email" => $info['email'],
			"password1" => $info['password1'],
			"password2" => $info['password2'],
			"invitecode" => $info['invitecode'],
			"__token__" => $info['__token__']
		];
		$validate = new Validate($rule, $msg);
		$check1 = $validate->check($data1);
		if (!$check1) {
			jsonReturn(2, $validate->getError());
		}
		//输入验证

		//注册验证
		/*
		 * 未完成清单
		 * 1、关闭邀请注册（某等级及以上在关闭邀请时依旧可以邀请等）
		 */
		//重复检查
		$check2 = db("users")->where(function ($query) use ($info) {
			$query->where("username", $info['username'])->whereOr("email", $info['email']);
		})->where("softdel", "0")->value("uid");
		if ($check2 <= 0) {
			$rnd1 = mt_rand();
			$rnd2 = mt_rand();
			$safekey = md5(md5($rnd1 . $info['username'] . $rnd2 . microtime() . $rnd1));
			$passsha = md5(md5($safekey . $info['password1'] . $info['username'] . $safekey));
			$uuid = random(3, 21, $safekey . $info['username']);
			$extract = random(15, 13);
			$appid = random(11, 9);
			$appkey = random(13, 21);
			if ($info['invitecode']) {
				$check3 = db("invite")->where("invitecode", $info['invitecode'])->where("softdel", "0")->value("uid");
				if ($check3 > 0) {
					$inviteby = $check3;
					db("invite")->where("invitecode", $info['invitecode'])->update(["softdel" => "1", "username" => $info['username']]);
				} else {
					jsonReturn(0, "该邀请码不存在");
				}
			}
			$data2 = [
				"username" => $info['username'],
				"email" => $info['email'],
				"passsha" => $passsha,
				"uuid" => $uuid,
				"safekey" => $safekey,
				"addtime" => nowtime(),
				"inviteby" => isset($inviteby) ? $inviteby : 0,
				"extract" => $extract,
				"appid" => $appid,
				"appkey" => $appkey,
				"ip" => ip(),
				"price" => PRICE,
				"uploaded" => UPLOAD,
				"downloaded" => DOWNLOAD,
				"longinv" => INV
			];
			//是否开启邮件确认
			if (ENABLED_VERIFY == "on") {
				$data2 = array_merge($data2, ["confirm" => 1]);
				$jwt = verify::encode($info['email'], 86400);
				//可以自己修改邮件内容，支持HTML ↓
				$msg = "请在1天内点击链接并激活帐号：<a href=" . getWebURL() . url("/index/login/doConfirmUser", ["verify" => $jwt]) . " target='_blank'>点我点我</a>";
				//可以自己修改邮件内容，支持HTML ↑
				mailqueue::mailqueue($info['email'], "帐号激活", $msg);
			}
			if (DISABLED_REG == "off") {
				$uid = db("users")->insertGetId($data2);
			} else {
				if (!empty($inviteby)) {
					$uid = db("users")->insertGetId($data2);
				}
			}
			if (!empty($uid)) {
				jsonReturn(1);
			} else {
				jsonReturn(3);
			}
		} else {
			jsonReturn(0, "该账号或邮箱已存在，请重试");
		}
		//注册验证
	}
}